package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Card;

@Service
public class CardDAOImpl implements CardDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public List<Card> getCards() {
		List<Card> cards = null;
		TypedQuery<Card> query = entityManager.createQuery(
				"Select c from Card c", Card.class);
		cards = query.getResultList();
		return cards;
	}

	@Transactional
	public boolean addCard(Card card) {
		try {
			entityManager.persist(card);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Transactional
	public List<Card> getCards(String loginId) {
		List<Card> cards = null;
		TypedQuery<Card> query = entityManager.createQuery(
				"Select c from Card c Where c.customer.loginId = :lg",
				Card.class);
		query.setParameter("lg", loginId);
		cards = query.getResultList();
		return cards;
	}

}
